// pages/admin/plugins/feedback/reply/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    tempFilePaths: [],
    uploadImg: [],
    info: {
      remarks: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
  },

  bindTextAreaBlur(e){
    this.setData({
      ['info.remarks']: e.detail.value
    })
  },

  //上传图片
  upload: function () {
    var that = this;
    var pics = this.data.tempFilePaths;
    wx.chooseImage({
      count: 3 - pics.length,
      success: function (res) {
        console.log(res);
        pics.push.apply(pics, res.tempFilePaths);
        that.setData({
          tempFilePaths: pics
        })
      },
    })
  },

  //浏览图片
  preview: function (e) {
    var index = e.currentTarget.dataset.index;
    var url = this.data.tempFilePaths
    wx.previewImage({
      urls: url,
      current: url[index]
    })
  },

  //删除图片
  deleteImg: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(index)
    var temp = this.data.tempFilePaths;
    temp.splice(index, 1);
    this.setData({
      tempFilePaths: temp
    })
  },


  // 提交工单
  createWorkList: function () {
    var that = this;
    if (this.data.info.remarks === '') {
      app.showConfirm('提示', '请输入详细情况', false);
    } else {
      that.data.uploadImg = [];
      wx.showLoading({
        title: '加载中...',
        mask: true
      })
      if (that.data.tempFilePaths != null && that.data.tempFilePaths != '' && that.data.tempFilePaths.length != 0) {
        for (var i = 0; i < that.data.tempFilePaths.length; i++) {
          if (that.data.tempFilePaths[i].indexOf('wxfile://') > -1 || that.data.tempFilePaths[i].indexOf('http://tmp') > -1) {
            app.server.uploadImage(that.data.tempFilePaths[i], {
              work_order_type: 2
            }).then(function (res) {
              that.data.uploadImg.push(res.data.url)
              if (that.data.uploadImg.length == that.data.tempFilePaths.length) {
                that.submitFeedback();
              }
            }, function (res) {
              app.showConfirm('提示', '图片上传失败，请重试', false);
              wx.hideLoading();
            });
          }
        }
      } else {
        that.submitFeedback();
      }
    }
  },

  submitFeedback() {
    var data = {
      id: this.data.id,
      point_url: app.server.adminApi.doReplyFeedback,
      remarks: this.data.info.remarks,
      annex: JSON.stringify(this.data.uploadImg)
    };
    app.server.getConnect(data).then(function (res) {
      console.log(res);
      app.showConfirm('提示', '反馈提交成功', false, function () {
        wx.navigateBack();
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})